/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLNV.Bean;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import QLNV.Entity.User;
import QLNV.Utilities.*;

import java.sql.*;
import java.util.*;

/**
 *
 * @author Ice
 */
public class LoginForm extends org.apache.struts.action.ActionForm {
    
    private String name;
    private int number;
    private String username;
    private String password;

    /**
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * @param string
     */
    public void setName(String string) {
        name = string;
    }

    /**
     * @return
     */
    public int getNumber() {
        return number;
    }

    /**
     * @param i
     */
    public void setNumber(int i) {
        number = i;
    }

    /**
     *
     */
    public LoginForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getName() == null || getName().length() < 1) {
            errors.add("name", new ActionMessage("error.name.required"));
            // TODO: add 'error.name.required' key to your resources
        }
        return errors;
    }
    
//    public boolean checkLogin (String username, String password) throws SQLException{
//
//        try{
//            Conn cnn = new Conn(); //DB connection
//            String sql = "Select * from USER where username='"+username+"'"+" and password='"+password+"'";
//            ResultSet rs = cnn.exec(sql);
//            if(rs==null)
//                return false;
//            else 
//                return true;
//        }
//        catch(Exception e){
//            e.getMessage();
//        }
//        return false;
//    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
    /**
     * 
     * @param name
     * @param password
     * @return
     * @throws SQLException 
     */
    public User checkLogin(String username, String password) throws SQLException {
        User user = new User();
        int usertype = 0;
        //String userid;
        DBHelper.getInstance().initConnection();
        ArrayList<String> param = new ArrayList<String>();
        param.add(username);
        param.add(password);
        ResultSet rs = DBHelper.getInstance().executeQueryProc("CheckLogin", param);
        if (rs != null) {
            while (rs.next()) {
                ResultSetMetaData rsmd = rs.getMetaData();
                int numberOfColumns = rsmd.getColumnCount();
                if (numberOfColumns == 1) {
                    user.setMessage(rs.getString("message"));
                } else {
                    usertype = rs.getInt("usertype");
                    //userid = rs.getString("userid");
                    //user.setUsername(userid);
                    user.setUsertype(usertype);
                }
            }
            DBHelper.getInstance().closeConnection();
        }
        return user;
    }
}
